package NG::Sys::App::AdminReference;
use strict;
use warnings;
use utf8;

use Data::Dumper;
use JSON::XS;

use base 'NG::Sys::Hnd';
use NG::Sys::Cached::DBReference;
use NG::Sys::Cfg;

my $t;
my $data;
my %data_all;
my $page_size;
my $config;
my $description;
my @list_reference;
sub _init {
	my $pkg = shift;
	my $app = shift;
	$config = config("reference");
	$description = config("reference_description");
	#$app->logger->info(Dumper($description));
	$t = $app->template_tt->read($app->{template});
	$page_size = $app->{page_size} || 10;
	while(my($n,$t) =  each %{$config->{table}}){
		$app->logger->notice("n=$n t=".$description->{$n}{namespace});
		push @list_reference,[$n,$description->{$n}{namespace}];
	}
	@list_reference = sort {$a->[1] cmp $b->[1]} @list_reference;
}
sub on_before_process {
	my ($app,$form) = @_;
	my $ref;
	unless($ref = $form->{input}{ref}){
		$ref = $form->{input}{ref} = $list_reference[0][0];
	}
	unless($form->{data}{ref} = $ref){
		die "bad config, no references";
	}
	unless($config->{table}{$ref}){
		die "bad references '$ref'";
	}
	unless($data = $data_all{$ref}){
		$data = $data_all{$ref} = new NG::Sys::Cached::DBReference(
			dbh => $app->dbh,
			app => $app,
			cache => $config->{cache},
			%{$config->{table}{$ref}}
		);
	}
	return 1;
}

sub cmd_default{
	my ($app,$form,$obj) = @_;
	my $ref = $form->{input}{ref};
	$form->{data}{list} = \@list_reference;
	$form->{data}{table} = $config->{table}{$ref};
	$form->{data}{description} = $description->{$ref};
	$form->{data}{last_new} = $data->last_new || 0;
	$form->{data}{now_time} = time();
	$form->{data}{path_to_img} = $app->{cfg}{path_to_img}.$ref."/";
	require CGI;
	$form->{data}{upl} = $CGI::DISABLE_UPLOADS;
	$form->process($t);
}
sub cmd_page{
	my ($app,$form,$obj) = @_;
	$data->_read_data(1);
	my $list = $data->get_list;
	my $lp = int($#$list / $page_size);
	$lp = 0 if $lp < 0;
	
	my $page = $form->{input}{page} || 0;
	if($page < 0){
		$page = $lp;
	}elsif($page > $lp){
		$page = $lp;
	}

	my $bi = $page * $page_size;
	my $ei = $bi + $page_size - 1;
	if($ei > $#$list){
		$ei = $#$list;
	}
	$form->{data}{list} = [@{$list}[$bi .. $ei]];
	$form->{data}{c_page} = $page;
	$form->{data}{f_page} = 0;
	$form->{data}{l_page} = $lp;
	$form->process_as_json;
}
sub cmd_save {
	my ($app,$form,$obj) = @_;
	my $id;

	my $row = $form->{input}{row};
	$app->logger->notice(Dumper($form->{input}));

	if($id = $row->{$data->{pkey}}){
		$data->update_row($row,1);
	}else{
		$id = $data->save_row($row);
		$form->{input}{page} = -1;
	}
	$app->do_action($form,["page"]);
}
sub cmd_save2 {
	my ($app,$form,$obj) = @_;
	my $id;

	my $row = $form->{input}{row};
	if($row){
		$row = decode_json($row);
		$app->logger->notice(Dumper($row));
	
		if($id = $row->{$data->{pkey}}){
			$data->update_row($row,1);
		}else{
			$id = $data->save_row($row);
			$form->{input}{page} = -1;
		}
	}
	$app->do_action($form,["page"]);
}
sub cmd_delete {
	my ($app,$form,$obj) = @_;
	my $id;

	my $row_id = $form->{input}{row_id};
	unless(ref $row_id){
		$row_id = [$row_id];
	}
	for(@$row_id){
		$data->remove_row($_);
	}
	$app->do_action($form,["page"]);
}
sub cmd_renew {
	my ($app,$form,$obj) = @_;
	$data->renew;
	$app->do_action($form,["default"]);
}
sub cmd_export {
	my ($app,$form,$obj) = @_;
	$data->_read_data(1);
	my $list = $data->get_list;
	$form->set_header('-type' => "text/plain");
	$form->set_header('Content-Disposition' => "attachment; filename=\"".$form->{input}{ref}.".txt\"");
	require JSON::XS;
	my $str = JSON::XS->new->pretty(1)->encode($list);
	$str =~ s/\n/\r\n/g;
	$form->output($str);
}
sub cmd_import {
	my ($app,$form,$obj) = @_;
	my $result;
	if(my $file = $form->{input}{file_for_import}){
		$form->{data}{file} = $file->{filename};
		$form->{data}{filetype} = $file->{type};
		if(my $filedata = $file->{content}()){
			my $ref;
			$filedata =~ s/^[^\[]+//;
			eval {
				$ref = JSON::XS::decode_json($filedata);
			};
			if($@){
				$result = $@;
			}else{
				$result = "success";
				$form->{data}{filedata} = $ref;
				for my $row(@$ref){
					$data->save_row($row);
				}
			}
		}else{
			$result = "no data";
		}
	}else{
		$result = "no file";
	}
	$form->{data}{result} = $result;
	$app->do_action($form,["default"]);
}
1;
